#include "idcard_helper.h"

#include <QDate>

static int WEIGHT_MAP_ARRAY[] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
static char CHECK_CHARACTER_ARRAY[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

bool IdCardHelper::checkValidDate(int year, int month, int dayOfMonth) {
    QDate date = QDate();
    bool isValidDate = date.isValid(year, month, dayOfMonth);
    return isValidDate;
}

char IdCardHelper::calculateLastChar(QString &idCardNo) {
    int sum = 0;
    for (int i = 0; i < 17; i++) {
        int singleNum = idCardNo.midRef(i, 1).toInt();
        sum += singleNum * WEIGHT_MAP_ARRAY[i];
    }
    int remainder = sum % 11;
    return CHECK_CHARACTER_ARRAY[remainder];
}
